package com.bz.sunmipay.payment.emv;

import android.util.Log;

import com.bz.sunmipay.application.MyApplication;
import com.bz.sunmipay.utils.ByteUtil;
import com.bz.sunmipay.utils.Constant;
import com.bz.sunmipay.utils.LogUtil;
import com.sunmi.pay.hardware.aidlv2.AidlConstantsV2;
import com.sunmi.pay.hardware.aidlv2.bean.AidV2;
import com.sunmi.pay.hardware.aidlv2.bean.CapkV2;
import com.sunmi.pay.hardware.aidlv2.bean.EmvTermParamV2;
import com.sunmi.pay.hardware.aidlv2.emv.EMVOptV2;
import com.sunmi.pay.hardware.aidlv2.security.SecurityOptV2;

import java.util.HashMap;
import java.util.Map;

public final class EmvUtil {

    public static final String COUNTRY_CHINA = "China";
    public static final String COUNTRY_RUSSIA = "Russia";
    public static final String COUNTRY_INDIA = "India";

    private EmvUtil() {
    }

    /**
     * Get configuration by country
     */
    public static Map<String, String> getConfig(String country) {
        Map<String, String> map = new HashMap<>();
        map.put("countryCode", "0156");     // country code(国家代码)
        map.put("capability", "E0F8C8");    // capability(终端性能)
        map.put("5F2A", "0156");            // transaction currency code(交易货币代码)
        map.put("5F36", "00");              // transaction currency code exponent(交易货币代码指数)
        switch (country) {
            case COUNTRY_RUSSIA:
                map.put("countryCode", "0643");
                map.put("capability", "E0F8C8");
                map.put("5F2A", "0643");
                map.put("5F36", "00");
                break;
            case COUNTRY_INDIA:
                map.put("countryCode", "0356");
                map.put("capability", "E0F8C8");
                map.put("5F2A", "0356");
                map.put("5F36", "02");
                break;
        }
        return map;
    }

    /**
     * Initialize keys
     */
    public static void initKey() {
        try {
            SecurityOptV2 mSecurityOptV2 = MyApplication.mSecurityOptV2;

            byte[] cvByte = ByteUtil.hexStr2Bytes("82E13665B4624DF5");

            // save KEK
            byte[] dataByte = ByteUtil.hexStr2Bytes("11111111111111111111111111111111");
            int result = mSecurityOptV2.savePlaintextKey(AidlConstantsV2.Security.KEY_TYPE_KEK, dataByte, cvByte, AidlConstantsV2.Security.KEY_ALG_TYPE_3DES, 10);
            Log.e(Constant.TAG, "save KEK result:" + result);
            if (result != 0) {
                Log.e(Constant.TAG, "save KEK fail");
                return;
            }

            // save TMK
            dataByte = ByteUtil.hexStr2Bytes("F40379AB9E0EC533F40379AB9E0EC533");
            result = mSecurityOptV2.saveCiphertextKey(AidlConstantsV2.Security.KEY_TYPE_TMK, dataByte, cvByte, 10, AidlConstantsV2.Security.KEY_ALG_TYPE_3DES, 11);
            Log.e(Constant.TAG, "save TMK result:" + result);
            if (result != 0) {
                Log.e(Constant.TAG, "save TMK fail");
                return;
            }

            // save PIK
            result = mSecurityOptV2.saveCiphertextKey(AidlConstantsV2.Security.KEY_TYPE_PIK, dataByte, cvByte, 11, AidlConstantsV2.Security.KEY_ALG_TYPE_3DES, 12);
            Log.e(Constant.TAG, "save PIK result:" + result);
            if (result != 0) {
                Log.e(Constant.TAG, "save PIK fail");
                return;
            }

            // save MAK
            result = mSecurityOptV2.saveCiphertextKey(AidlConstantsV2.Security.KEY_TYPE_MAK, dataByte, cvByte, 11, AidlConstantsV2.Security.KEY_ALG_TYPE_3DES, 13);
            LogUtil.e(Constant.TAG, "save MAK result:" + result);
            if (result != 0) {
                LogUtil.e(Constant.TAG, "save MAK fail");
                return;
            }

            // save TDK
            result = mSecurityOptV2.saveCiphertextKey(AidlConstantsV2.Security.KEY_TYPE_TDK, dataByte, cvByte, 11, AidlConstantsV2.Security.KEY_ALG_TYPE_3DES, 14);
            LogUtil.e(Constant.TAG, "save TDK result:" + result);
            if (result != 0) {
                LogUtil.e(Constant.TAG, "save TDK fail");
                return;
            }

            LogUtil.e(Constant.TAG, "init key success");
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(Constant.TAG, "init key fail");
        }
    }

    /**
     * Initialize AIDs and RIDs
     */
    public static void initAidAndRid() {
        try {
            EMVOptV2 emvOptV2 = MyApplication.mEMVOptV2;
            // Normal AIDs
            AidV2 aid = EmvUtil.hexStr2Aid("9F0608A000000333010101DF0101009F08020020DF1105D84000A800DF1205D84004F800DF130500100000009F1B040000C350DF150400000000DF160199DF170199DF14039F3704DF1801019F7B06100000000000DF1906100000000000DF2006100000000000DF21061000000000005F2A020643");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0608A000000333010102DF0101009F08020020DF1105D84000A800DF1205D84004F800DF130500100000009F1B040000C350DF150400000000DF160199DF170199DF14039F3704DF1801019F7B06100000000000DF1906100000000000DF2006100000000000DF21061000000000005F2A020643");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0608A000000333010103DF0101009F08020020DF1105D84000A800DF1205D84004F800DF130500100000009F1B040000C350DF150400000000DF160199DF170199DF14039F3704DF1801019F7B06100000000000DF1906100000000000DF2006100000000000DF21061000000000005F2A020643");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0608A000000333010106DF0101009F08020020DF1105D84000A800DF1205D84004F800DF130500100000009F1B040000C350DF150400000000DF160199DF170199DF14039F3704DF1801019F7B06100000000000DF1906100000000000DF2006100000000000DF21061000000000005F2A020643");
            emvOptV2.addAid(aid);
            // Russia AIDs
            aid = EmvUtil.hexStr2Aid("9F0607A00000000410109F09020000DF11050000000000DF12050000000000DF130500000000009F1B0400000000DF150400000000DF160100DF170100DF140b9F37049F47018F019F32019F3C0206439F3D0102DF1906000000000000DF2006999999999999DF21060000000030009F4E009F150211119F160c746573746D65726368616E74");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0607A00000000310109F09020000DF11050000000000DF12050000000000DF130500000000009F1B0400000000DF150400000000DF160100DF170100DF14039F37049F3C0206439F3D0102DF1906000000000000DF2006999999999999DF21060000000000009F4E009F150211119F160c746573746D65726368616E74");
            emvOptV2.addAid(aid);
            // India AIDs
            aid = EmvUtil.hexStr2Aid("9F0607A00000052410109F09020064DF1105FFFFFFFFFFDF1205FFFFFFFFFFDF130500000000009F1B0400000000DF150400000000DF160100DF170100DF14039F37049F3C0203569F3D0102DF1906000000000000DF2006999999999999DF21060000000030009F4E009F15009F1600");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0607A00000015230109F09020001DF1105FFFFFFFFFFDF1205FFFFFFFFFFDF130500000000009F1B0400000000DF150400000000DF160100DF170100DF14039F37049F3C0203569F3D0102DF1906000000000000DF2006999999999999DF21060000000000009F4E009F15009F1600");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0607A00000006510109F09020200DF1105FFFFFFFFFFDF1205FFFFFFFFFFDF130500000000009F1B0400000000DF150400000000DF160100DF170100DF14039F37049F3C0203569F3D0102DF1906000000000000DF2006999999999999DF21060000000000009F4E009F15009F1600");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0608A0000003330101019F09020200DF1105FFFFFFFFFFDF1205FFFFFFFFFFDF130500000000009F1B0400000000DF150400000000DF160100DF170100DF14039F37049F3C0203569F3D0102DF1906000000000000DF2006999999999999DF21060000000000009F4E009F15009F1600");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0608A0000003330101029F09020200DF1105FFFFFFFFFFDF1205FFFFFFFFFFDF130500000000009F1B0400000000DF150400000000DF160100DF170100DF14039F37049F3C0203569F3D0102DF1906000000000000DF2006999999999999DF21060000000000009F4E009F15009F1600");
            emvOptV2.addAid(aid);
            aid = EmvUtil.hexStr2Aid("9F0608A0000003330101039F09020200DF1105FFFFFFFFFFDF1205FFFFFFFFFFDF130500000000009F1B0400000000DF150400000000DF160100DF170100DF14039F37049F3C0203569F3D0102DF1906000000000000DF2006999999999999DF21060000000000009F4E009F15009F1600");
            emvOptV2.addAid(aid);

            // Normal RIDs
            CapkV2 capkV2 = EmvUtil.hexStr2Rid("9F0605A0000003339F220104DF05083230323531323331DF060101DF070101DF0281F8BC853E6B5365E89E7EE9317C94B02D0ABB0DBD91C05A224A2554AA29ED9FCB9D86EB9CCBB322A57811F86188AAC7351C72BD9EF196C5A01ACEF7A4EB0D2AD63D9E6AC2E7836547CB1595C68BCBAFD0F6728760F3A7CA7B97301B7E0220184EFC4F653008D93CE098C0D93B45201096D1ADFF4CF1F9FC02AF759DA27CD6DFD6D789B099F16F378B6100334E63F3D35F3251A5EC78693731F5233519CDB380F5AB8C0F02728E91D469ABD0EAE0D93B1CC66CE127B29C7D77441A49D09FCA5D6D9762FC74C31BB506C8BAE3C79AD6C2578775B95956B5370D1D0519E37906B384736233251E8F09AD79DFBE2C6ABFADAC8E4D8624318C27DAF1DF040103DF0314F527081CF371DD7E1FD4FA414A665036E0F5E6E5");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000003339F220103DF05083230323431323331DF060101DF070101DF0281B0B0627DEE87864F9C18C13B9A1F025448BF13C58380C91F4CEBA9F9BCB214FF8414E9B59D6ABA10F941C7331768F47B2127907D857FA39AAF8CE02045DD01619D689EE731C551159BE7EB2D51A372FF56B556E5CB2FDE36E23073A44CA215D6C26CA68847B388E39520E0026E62294B557D6470440CA0AEFC9438C923AEC9B2098D6D3A1AF5E8B1DE36F4B53040109D89B77CAFAF70C26C601ABDF59EEC0FDC8A99089140CD2E817E335175B03B7AA33DDF040103DF031487F0CD7C0E86F38F89A66F8C47071A8B88586F26");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000003339F220102DF05083230323131323331DF060101DF070101DF028190A3767ABD1B6AA69D7F3FBF28C092DE9ED1E658BA5F0909AF7A1CCD907373B7210FDEB16287BA8E78E1529F443976FD27F991EC67D95E5F4E96B127CAB2396A94D6E45CDA44CA4C4867570D6B07542F8D4BF9FF97975DB9891515E66F525D2B3CBEB6D662BFB6C3F338E93B02142BFC44173A3764C56AADD202075B26DC2F9F7D7AE74BD7D00FD05EE430032663D27A57DF040103DF031403BB335A8549A03B87AB089D006F60852E4B8060");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000003339F220101DF05083230313431323331DF060101DF070101DF028180BBE9066D2517511D239C7BFA77884144AE20C7372F515147E8CE6537C54C0A6A4D45F8CA4D290870CDA59F1344EF71D17D3F35D92F3F06778D0D511EC2A7DC4FFEADF4FB1253CE37A7B2B5A3741227BEF72524DA7A2B7B1CB426BEE27BC513B0CB11AB99BC1BC61DF5AC6CC4D831D0848788CD74F6D543AD37C5A2B4C5D5A93BDF040103DF0314E881E390675D44C2DD81234DCE29C3F5AB2297A0");
            emvOptV2.addCapk(capkV2);
            // Russia RIDs
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000049F2201FADF0503202001DF060101DF070101DF028190A90FCD55AA2D5D9963E35ED0F440177699832F49C6BAB15CDAE5794BE93F934D4462D5D12762E48C38BA83D8445DEAA74195A301A102B2F114EADA0D180EE5E7A5C73E0C4E11F67A43DDAB5D55683B1474CC0627F44B8D3088A492FFAADAD4F42422D0E7013536C3C49AD3D0FAE96459B0F6B1B6056538A3D6D44640F94467B108867DEC40FAAECD740C00E2B7A8852DDF040103DF03145BED4068D96EA16D2D77E03D6036FC7A160EA99C");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000049F2201F1DF0503202001DF060101DF070101DF0281b0A0DCF4BDE19C3546B4B6F0414D174DDE294AABBB828C5A834D73AAE27C99B0B053A90278007239B6459FF0BBCD7B4B9C6C50AC02CE91368DA1BD21AAEADBC65347337D89B68F5C99A09D05BE02DD1F8C5BA20E2F13FB2A27C41D3F85CAD5CF6668E75851EC66EDBF98851FD4E42C44C1D59F5984703B27D5B9F21B8FA0D93279FBBF69E090642909C9EA27F898959541AA6757F5F624104F6E1D3A9532F2A6E51515AEAD1B43B3D7835088A2FAFA7BE7DF040103DF0314D8E68DA167AB5A85D8C3D55ECB9B0517A1A5B4BB");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000049F2201EFDF0503202001DF060101DF070101DF0281f8A191CB87473F29349B5D60A88B3EAEE0973AA6F1A082F358D849FDDFF9C091F899EDA9792CAF09EF28F5D22404B88A2293EEBBC1949C43BEA4D60CFD879A1539544E09E0F09F60F065B2BF2A13ECC705F3D468B9D33AE77AD9D3F19CA40F23DCF5EB7C04DC8F69EBA565B1EBCB4686CD274785530FF6F6E9EE43AA43FDB02CE00DAEC15C7B8FD6A9B394BABA419D3F6DC85E16569BE8E76989688EFEA2DF22FF7D35C043338DEAA982A02B866DE5328519EBBCD6F03CDD686673847F84DB651AB86C28CF1462562C577B853564A290C8556D818531268D25CC98A4CC6A0BDFFFDA2DCCA3A94C998559E307FDDF915006D9A987B07DDAEB3BDF040103DF031421766EBB0EE122AFB65D7845B73DB46BAB65427A");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000049F220147DF0503202001DF060101DF070101DF028190F562B594C911C0310AEFB002BDFE01CC8C2F1351CAEDD12C5210F4C3507BC106E01BBF94362392F3A66496079CBC8EAD09D5D942B195CA12A8A1AABAEF1D5545AF1693484764DEB625100E15F630510F3E2FB9E00D81F86D4C6F099307182E30F67FBE9C3D91F557BA5AAA1ACFBB625ABFA0176D4CEF3DFE1892BA1EC49F57B5DB6C53147F1DC9FD844298E238A94FEBDF040103DF03146304B43BAF1342F914ABD4B55EE817CEF1FA557F");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000049F2201F5DF0503202001DF060101DF070101DF0281f8A6E6FB72179506F860CCCA8C27F99CECD94C7D4F3191D303BBEE37481C7AA15F233BA755E9E4376345A9A67E7994BDC1C680BB3522D8C93EB0CCC91AD31AD450DA30D337662D19AC03E2B4EF5F6EC18282D491E19767D7B24542DFDEFF6F62185503532069BBB369E3BB9FB19AC6F1C30B97D249EEE764E0BAC97F25C873D973953E5153A42064BBFABFD06A4BB486860BF6637406C9FC36813A4A75F75C31CCA9F69F8DE59ADECEF6BDE7E07800FCBE035D3176AF8473E23E9AA3DFEE221196D1148302677C720CFE2544A03DB553E7F1B8427BA1CC72B0F29B12DFEF4C081D076D353E71880AADFF386352AF0AB7B28ED49E1E672D11F9DF0403010001DF0314C2239804C8098170BE52D6D5D4159E81CE8466BF");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000049F2201F7DF0503202001DF060101DF070101DF0281f8B8C5759F96676BEF1FE2DE8AAD8C54B08E919FD1B8C516D70931435E41A306A64A5F73E4542B46BFBDC8EB32E973737406C0DE7AF80FDE023B5F256EF4D120EEB537171D1ADA4D83C307961AF8DB13B63828EE2EE036FAF2C41D5EE250ED2A4AF317511411EDD39CD52F2A10CA17F4FE652FD7E79BC031C36AD26DFB54CFC562551793B04A47ED1A21E96D2E4CA29EB9A4FE6524E6CF69DE5CA5C0BEDFA624AE6BC78FEFE3993564D59B67DED46455040FB3BFD2B6447D111C83C0E29C72C00142BAB87EBCFC54BE58EAEE78292B05F358175DC67F3916CCB08907A41EF0785DAA4D21B279B9A386AB7F6624B2144E3E35C1BF29954B2D7BDF040103DF031486803714DF3BDE4C19F62E1FF01CE96D3C1D1706");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000049F220105DF0503202001DF060101DF070101DF0281b0B8048ABC30C90D976336543E3FD7091C8FE4800DF820ED55E7E94813ED00555B573FECA3D84AF6131A651D66CFF4284FB13B635EDD0EE40176D8BF04B7FD1C7BACF9AC7327DFAA8AA72D10DB3B8E70B2DDD811CB4196525EA386ACC33C0D9D4575916469C4E4F53E8E1C912CC618CB22DDE7C3568E90022E6BBA770202E4522A2DD623D180E215BD1D1507FE3DC90CA310D27B3EFCCD8F83DE3052CAD1E48938C68D095AAC91B5F37E28BB49EC7ED597DF040103DF0314EBFA0D5D06D8CE702DA3EAE890701D45E274C845");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000039F220195DF0503202001DF060101DF070101DF028190BE9E1FA5E9A803852999C4AB432DB28600DCD9DAB76DFAAA47355A0FE37B1508AC6BF38860D3C6C2E5B12A3CAAF2A7005A7241EBAA7771112C74CF9A0634652FBCA0E5980C54A64761EA101A114E0F0B5572ADD57D010B7C9C887E104CA4EE1272DA66D997B9A90B5A6D624AB6C57E73C8F919000EB5F684898EF8C3DBEFB330C62660BED88EA78E909AFF05F6DA627BDF040103DF0314EE1511CEC71020A9B90443B37B1D5F6E703030F6");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000039F220199DF0503202001DF060101DF070101DF028180AB79FCC9520896967E776E64444E5DCDD6E13611874F3985722520425295EEA4BD0C2781DE7F31CD3D041F565F747306EED62954B17EDABA3A6C5B85A1DE1BEB9A34141AF38FCF8279C9DEA0D5A6710D08DB4124F041945587E20359BAB47B7575AD94262D4B25F264AF33DEDCF28E09615E937DE32EDC03C54445FE7E382777DF040103DF03144ABFFD6B1C51212D05552E431C5B17007D2F5E6D");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000039F220150DF0503202001DF060101DF070101DF028180D11197590057B84196C2F4D11A8F3C05408F422A35D702F90106EA5B019BB28AE607AA9CDEBCD0D81A38D48C7EBB0062D287369EC0C42124246AC30D80CD602AB7238D51084DED4698162C59D25EAC1E66255B4DB2352526EF0982C3B8AD3D1CCE85B01DB5788E75E09F44BE7361366DEF9D1E1317B05E5D0FF5290F88A0DB47DF040103DF03145765295089960938BAAA4431506E424295F98BD0");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000039F220192DF0503202001DF060101DF070101DF0281b0996AF56F569187D09293C14810450ED8EE3357397B18A2458EFAA92DA3B6DF6514EC060195318FD43BE9B8F0CC669E3F844057CBDDF8BDA191BB64473BC8DC9A730DB8F6B4EDE3924186FFD9B8C7735789C23A36BA0B8AF65372EB57EA5D89E7D14E9C7B6B557460F10885DA16AC923F15AF3758F0F03EBD3C5C2C949CBA306DB44E6A2C076C5F67E281D7EF56785DC4D75945E491F01918800A9E2DC66F60080566CE0DAF8D17EAD46AD8E30A247C9FDF040103DF0314429C954A3859CEF91295F663C963E582ED6EB253");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000000039F220194DF0503202001DF060101DF070101DF0281f8ACD2B12302EE644F3F835ABD1FC7A6F62CCE48FFEC622AA8EF062BEF6FB8BA8BC68BBF6AB5870EED579BC3973E121303D34841A796D6DCBC41DBF9E52C4609795C0CCF7EE86FA1D5CB041071ED2C51D2202F63F1156C58A92D38BC60BDF424E1776E2BC9648078A03B36FB554375FC53D57C73F5160EA59F3AFC5398EC7B67758D65C9BFF7828B6B82D4BE124A416AB7301914311EA462C19F771F31B3B57336000DFF732D3B83DE07052D730354D297BEC72871DCCF0E193F171ABA27EE464C6A97690943D59BDABB2A27EB71CEEBDAFA1176046478FD62FEC452D5CA393296530AA3F41927ADFE434A2DF2AE3054F8840657A26E0FC617DF040103DF0314C4A3C43CCF87327D136B804160E47D43B60E6E0F");
            emvOptV2.addCapk(capkV2);
            // India RIDs
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000005249F22016BDF028190C9DFDB625ADA4B5E86049F85A0237627B59524F52BD499B4C5482C1EE012D61A1446E9383CC0B7EE2922D323A5ECDA12941EA8177CFA512DA6B5B7663A89B793B10D314CBB776EB96D0B1734EDE7E1591713915E9991B7B4E8A017A6901279AEBDD6136C9FE7E0C6CBF94C77FA606B629D00B1F890473905EB4DAD1AD93B29C2C1829A82F880B08986B9387611EE409DDF040103DF060101DF070101DF03149602428A46271C63CCC6DD99477CDB70435D6D5BDF0503201231");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000005249F22016ADF02818092795EAA4FE39EB30441FE952D5423778E02F86783B89DD7C587AE80A69F4D6DC55EAFB6604040D875C72002425EE529CE4EA26FD864BAD760160C2AA0C5AF92381894A5CBBC8AB3AF2641606C379B927A397CB1E9B9EA2EF8C0A9C0DDEBB81B0F8913A118F7044156EA7D23AF626EAF30C2C9ECE8534D3563EF5FE95DE76249DF040103DF060101DF070101DF031451ED4570323CD41A0348BDFEA81CCC0B8D9BAB3FDF0503201231");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000005249F22016CDF0281b0C76259FF785ABD5FF613223C01F5BDA0F36F9342CF336B66C32D4B2CD5096E094D8E04DFA11A9B2E3BC78DA63B5C10148D8ED79EBA685D5D0EFE1C58B3F929D861B40FF3AAA3B527148D0C24921EE42DA048E01E38F6A3A49DFA67DD1CD5DD2091412DD36D3269FAF7D2E0FFB1A3E028969CB6BA5A9303A6FF65540F421B069A31B553398EE525EFA5C2CE26BCB81C5345018D5E3E9B7130F72F598C0EAA4682D4DA2F2204518780A8108F82DDC9CF1FDF040103DF060101DF070101DF03143B18A21BF34F781208145D7567982513D1CE8C92DF0503201231");
            emvOptV2.addCapk(capkV2);
            capkV2 = EmvUtil.hexStr2Rid("9F0605A0000005249F22016DDF0281f8B747E8CB3615E8D26231355488F3C76C4746F7BB1C381E6C6E6ABF0A6D7CD93CFC6B2C310288CA8BE7EE1730DE621A59D1BB2D8C02C9148FA06E5D1F5E672EEFCE8AECBAD4A1C18F3175F1BEA1AEF539376592366B46A5044E32E59B3F35F50E85F843BA01851E5386B7EBE27367D3D483C5472D3020AF42116DDDA32341557EBABB043EBC6006B99A652009045BFA50C527028586E05942E1D594223B49FE8566931C31FBE8C903ABD4F283E1FAB03D758247EC4B728A85A9897601B753293263ADBD10BE988D0C52FE0091C2721DC02C5130FC7663E95739A70EE2F84DFD2E50C88A1A26587EF7CC047FCA2D03C2CF0CE4B524B4EC3F07DF040103DF060101DF070101DF0314411008F9921B89C62E2160F6D0358614115ECD4ADF0503201231");
            emvOptV2.addCapk(capkV2);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(Constant.TAG, "initAIDAndRid fail");
        }
    }

    /**
     * Set EMV terminal parameters
     */
    public static void setTerminalParam(Map<String, String> map) {
        try {
            EMVOptV2 emvOptV2 = MyApplication.mEMVOptV2;

            EmvTermParamV2 emvTermParam = new EmvTermParamV2();
            emvTermParam.countryCode = map.get("countryCode");
            emvTermParam.capability = map.get("capability");
            int result = emvOptV2.setTerminalParam(emvTermParam);
            LogUtil.e(Constant.TAG, "setTerminalParam result:" + result);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(Constant.TAG, "setTerminalParam fail");
        }
    }

    public static AidV2 hexStr2Aid(String hexStr) {
        AidV2 aidV2 = new AidV2();
        Map<String, TLV> map = TLVUtil.buildTLVMap(hexStr);
        TLV tlv = map.get("DF21");
        if (tlv != null) {
            aidV2.cvmLmt = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF20");
        if (tlv != null) {
            aidV2.termClssLmt = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF19");
        if (tlv != null) {
            aidV2.termClssOfflineFloorLmt = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F7B");
        if (tlv != null) {
            aidV2.termOfflineFloorLmt = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F06");
        if (tlv != null) {
            aidV2.aid = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF01");
        if (tlv != null) {
            aidV2.selFlag = ByteUtil.hexStr2Byte(tlv.getValue());
        }
        tlv = map.get("DF17");
        if (tlv != null) {
            aidV2.targetPer = ByteUtil.hexStr2Byte(tlv.getValue());
        }
        tlv = map.get("DF16");
        if (tlv != null) {
            aidV2.maxTargetPer = ByteUtil.hexStr2Byte(tlv.getValue());
        }
        tlv = map.get("9F1B");
        if (tlv != null) {
            aidV2.floorLimit = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF15");
        if (tlv != null) {
            aidV2.threshold = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF13");
        if (tlv != null) {
            aidV2.TACDenial = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF12");
        if (tlv != null) {
            aidV2.TACOnline = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF11");
        if (tlv != null) {
            aidV2.TACDefault = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F01");
        if (tlv != null) {
            aidV2.AcquierId = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF14");
        if (tlv != null) {
            aidV2.dDOL = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F09");
        if (tlv != null) {
            aidV2.version = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F4E");
        if (tlv != null) {
            aidV2.merchName = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F15");
        if (tlv != null) {
            aidV2.merchCateCode = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F16");
        if (tlv != null) {
            aidV2.merchId = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F3C");
        if (tlv != null) {
            aidV2.referCurrCode = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F3D");
        if (tlv != null) {
            aidV2.referCurrExp = ByteUtil.hexStr2Byte(tlv.getValue());
        }
        return aidV2;
    }

    public static CapkV2 hexStr2Rid(String hexStr) {
        CapkV2 capkV2 = new CapkV2();
        Map<String, TLV> map = TLVUtil.buildTLVMap(hexStr);
        TLV tlv = map.get("9F06");
        if (tlv != null) {
            capkV2.rid = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("9F22");
        if (tlv != null) {
            capkV2.index = ByteUtil.hexStr2Byte(tlv.getValue());
        }
        tlv = map.get("DF06");
        if (tlv != null) {
            capkV2.hashInd = ByteUtil.hexStr2Byte(tlv.getValue());
        }
        tlv = map.get("DF07");
        if (tlv != null) {
            capkV2.arithInd = ByteUtil.hexStr2Byte(tlv.getValue());
        }
        tlv = map.get("DF02");
        if (tlv != null) {
            capkV2.modul = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF04");
        if (tlv != null) {
            capkV2.exponent = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF05");
        if (tlv != null) {
            capkV2.expDate = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        tlv = map.get("DF03");
        if (tlv != null) {
            capkV2.checkSum = ByteUtil.hexStr2Bytes(tlv.getValue());
        }
        return capkV2;
    }


}
